package com.cai.kcsj.controller.pre;

import com.cai.kcsj.entity.User;
import com.cai.kcsj.service.LoginServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * @version 1.0
 * @author：蔡松坡
 * @date：2021/1/5 14 : 02
 * @package：com.cai.kcsj.controller.pre
 */
@Controller
@RequestMapping("/preLogin")
public class PreLogin {

    @Autowired
    private LoginServer loginServer;

    @RequestMapping("/preLogin")
    public String login(HttpServletRequest request, Model model, User user){
        User login = loginServer.login(user);
        if(login!=null){
            request.getSession().setAttribute("user",login);
            return "redirect:/preindex/preindex";
        }
        model.addAttribute("loginmsg","用户不存在");
        return "redirect:/router/toPreLogin";
    }

    @RequestMapping("/toPreReg")
    public String toPreReg(Model model,User user){
        user.setSf(0);
        boolean regist = loginServer.regist(user);
        if(regist){
            return "redirect:/router/toPreLogin";
        }
        model.addAttribute("msg","注册失败");
        return "redirect:/preLogin/toPreReg";
    }
}
